package practical.GrabRed;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 抢红包的算法
 * Created by tanlee on 2016/4/12.
 */
public class GrabRedEnvelope {

    /**
     * 输入金额获取红包的信息
     * 至少输入0.01 最多输出2倍
     * @param redPackage
     * @return
     */
    public List<Double> getListResult(RedPackage redPackage){
        redPackage = new RedPackage(redPackage);
        List<Double> result = new ArrayList<Double>();
        double min = 0.01;
        double max = redPackage.totalNum;
        double temp = 0.0;
        Random r = new Random();
        for(int i=0;i<redPackage.count;i++){
            if(i==redPackage.count - 1){
                result.add((double) redPackage.totalNum);
                break;
            }
            max = redPackage.totalNum / redPackage.count * 2;
            temp = r.nextDouble() * max;
            temp = temp<min ? 0.01:temp;
            redPackage.totalNum -= temp;
            result.add((double) temp);
        }
        return result;
    }

    /**
     * 打出红包的数字
     * @param redPackageNum 红包的总金额
     * @param redPackage 红包的输出结果
     */
    public void print(RedPackage redPackageNum,List<Double> redPackage){
        if(redPackageNum.count != redPackage.size()){
            System.out.println("Size is Error");
            return;
        }
        for(int i=0;i<redPackage.size();i++){
            redPackageNum.totalNum -= redPackage.get(i);
            System.out.println("" + i + "    :  " + redPackage.get(i) + "    remain :" + redPackageNum.totalNum + "\n");
        }
    }
}
